/*
US DIFFERENTIALs FOR PREDICTIONS
*/

* discounted returns
use "$datapath/Prediction_DiscountedReturns.dta", clear // discounted MSCI equity returns
merge 1:1 country datem using "$datapath/MSCI_exceldata_to_DTA.dta", gen(merge_msci) keepusing(ep dy) // dividend-price and earning-price

sencode country, gen(id) // encode the string variable "country" in a labeled numerical variable
xtset id datem
*****************************************************************************
* logarithm ep dy
foreach v of varlist ep dy {
	 gen ln_`v' = ln(`v')
}
*****************************************************************************

*****************************************************************************
*** variables of interest
global thevars = "ln_dy ln_ep r r1 r12 r36 r12d90 r36d93 r24 r24d89 r24d90 r24d92 r24d925 r24d93 r24d94 r36d90"
*****************************************************************************

*****************************************************************************
* U.S. variables
preserve
	qui keep if country == "unitedstates"
	foreach v of global thevars {
		qui rename `v' `v'_us
	}
	 qui keep *_us datem 
	 
	 tempfile theus
	 qui save `theus', replace
restore
merge m:1 datem using `theus'

* drop the U.S.
sort country datem
*****************************************************************************

*****************************************************************************
***  Differential w.r.t. US
foreach v of global thevars {
	qui gen dus_`v' =  `v' - `v'_us  		
}
*****************************************************************************

*****************************************************************************
* panel structure and lags
xtset id datem
*****************************************************************************

*****************************************************************************
* Lags
foreach v of varlist dus* {
	qui gen l1`v' =  l.`v'
	gen d`v' = `v' - l1`v'
}
*****************************************************************************

****************************************************************************

* sample selection 
drop if datem < ym(1969,12)

save "$datapath/Prediction_Differential.dta", replace
*end
*****************************************************************************